package com.sunday.volunteer.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.utils.BaseUtil;
import com.sunday.volunteer.utils.ViewPagerUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Pual's PC on 2016/6/15.
 */
public class InformationAdapter extends RecyclerView.Adapter {

    private List<Images> list;
    private  List<Images> list2;

    public InformationAdapter(List<Images> list ,List<Images> list2) {
        this.list = list;
        this.list2 = list2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case 0:
                View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewpager_header , null);
                return new HeaderViewHolder(headerView);
            default:
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_information , null);
                return new ViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position == 0){
            final HeaderViewHolder viewHolder = (HeaderViewHolder)holder;
            int width = DeviceUtils.getDisplay(viewHolder.viewPager.getContext()).widthPixels;
            if(list.size() == 0){
                viewHolder.lin.setVisibility(View.GONE);
                viewHolder.itemView.setLayoutParams(new LinearLayout.LayoutParams(width, 1));
                return;
            }
            viewHolder.itemView.setLayoutParams(new LinearLayout.LayoutParams(width, width * 9 / 20));
            new ViewPagerUtils(((HeaderViewHolder) holder).viewPager.getContext(), ((HeaderViewHolder) holder).viewPager,list, 0 , true);

        }else {
            ViewHolder viewHolder = (ViewHolder)holder;
            Images item = list2.get(position -1);
            viewHolder.itemView.setTag(item);
            Picasso.with(viewHolder.imageView.getContext()).load(item.getImage()).resize(180,140).placeholder(R.drawable.url_image_failed).into(viewHolder.imageView);
            viewHolder.name.setText(item.getTitle());
            viewHolder.time.setText(item.getViceTitle());
            //Log.d("image" , ImagePathUtils.NoUtil(item.getImage() , ""));
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return list2.size() +1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        @Bind(R.id.image_view)
        ImageView imageView;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.time)
        TextView time;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.item:
                    Images item = (Images) v.getTag();
//                    Intent intent = new Intent(v.getContext(), InformationDetailActivity.class);
//                    intent.putExtra("title", item.getTitle());
//                    intent.putExtra("url", SkyCityClient.API_URL +"/mobile/info/infoDetail?id=" +item.getId());
//                    intent.putExtra("image" ,ImagePathUtils.NoUtil(item.getImage() , ""));
//                    intent.putExtra("id" , item.getId());
//                    v.getContext().startActivity(intent);
                    BaseUtil.openWebViewActivity(v.getContext() , item.getUrl(), "志愿者");
                    break;
            }
        }

    }


    public class HeaderViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.convenientBanner)
        ConvenientBanner viewPager;
        @Bind(R.id.lin)
        LinearLayout lin;
        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
